/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.manager;

import com.google.inject.Inject;
import exatrez.stripes.util.EmailUtil;
import freemarker.cache.FileTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import java.io.File;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Mansur
 */
public class CronAdminManager {
    
    @Inject
    exatrez.stripes.Configuration conf;
    @Inject
    private EmailUtil emailUtil;
    
    private static Logger LOG = LoggerFactory.getLogger(CronAdminManager.class);
    
    public void sendEmail(){
        try{
            // freemarker stuff.
            Configuration cfg = new Configuration();
            DateFormat dateFormat = new SimpleDateFormat("dd/MMMMM/yyyy");
            DateFormat timeFormat = new SimpleDateFormat("H:mm:ss");
            Date newDate = new Date();
            FileTemplateLoader templateLoader 
                    = new FileTemplateLoader(new File(conf.getProperty("email.template.path")));
            cfg.setTemplateLoader(templateLoader);
            cfg.setObjectWrapper(new DefaultObjectWrapper());
            Template template = cfg.getTemplate("html-admin.ftl");
            Map<String, Object> rootMap = new HashMap<String, Object>();
            rootMap.put("date", dateFormat.format(newDate));
            rootMap.put("time", timeFormat.format(newDate));
            
            Writer out = new StringWriter();
            template.process(rootMap, out);
            String [] recipient = {conf.getProperty("email.systemAdmin")};
            emailUtil.send(recipient, null, "INFO : Scheduler Running.", out.toString(), "text/html");
            LOG.debug("INFO : CronJob Running.");
        }catch(Exception ex){
            LOG.error("error occured {}!! ", ex);
        }
    }
    
}
